package com.example.smart_video

import com.danikula.videocache.HttpProxyCacheServer
import com.wink_172.library.utils.CommonUtils.initUMeng
import com.wink_172.library.LibApplication
import org.xutils.x
import com.example.smart_video.model.Constants.saveSDPath
import com.example.smart_video.utils.MyFileNameGenerator
import com.wink_172.library.utils.TencentCosUtils.initTectentCos

class ABpplication : LibApplication() {
    override fun onCreate() {
        super.onCreate()
        initXutils()
        initUMeng(this, "60bd729f4d0228352bbd7180")
        //        CommonUtils.methodAliCloud();
        initRootPermissions()
        initVideoCacheProxy()


        initTectentCos(
            "AKIDCIH7mIBxAIcDl76k3GS1TkrmVELx2K70",
            "8JUYP4nPrAIEyVpwrZkkbFj071jGb5Oa",

        )
    }



    /**
     * 用户权限
     */
    private fun initRootPermissions() {
        val path1: String = saveSDPath + "root.txt"
        //        if (FileUtil.isCacheFileExist(path1)) {
//            String str=readTxt(path1);
//            Config config= JSON.parseObject(str,Config.class);
//            Constants.ROOT_MODE = config.isROOT_MODE();//超级用户
//            Constants.TENCENT_COS_MODE=config.isTENCENT_COS_MODE();
//        } else {
//            Constants.ROOT_MODE = false;//超级用户
//            Constants.TENCENT_COS_MODE=true;
//        }
    }

    /**
     * 1024 * 1024 * 1024=1G
     * 缓存位置   出现如下两种情况：1 Android/data/com.nufang.zao/cache/video-cache/
     *                       ： 2  cc:file:///data/user/0/com.nufang.zao/cache/video-cache/
     *                               file:///data/user/0/com.nufang.zao/cache/video-cache/o_1fnjae89dr4qg1n15ukfnd10nu9.mp4
     *
     *                xtuils主动下载目录       /storage/emulated/0/Android/data/com.nufang.zao/files/cache/video-cache/
     * **/
    private fun newCacheProxy(): HttpProxyCacheServer? {

//        Log.e(
//            TAG,
//            "onCreate: ====>>hello 1:" + Environment.getExternalStorageDirectory().absolutePath
//        )  storage/emulated/0
//        Log.e(
//            TAG,
//            "onCreate: ====>>hello 2:" + cacheDir.absolutePath
//        )  data/user/0/com.nufang.zao/cache
//        Log.e(TAG, "onCreate: ====>>hello 3:"+ externalCacheDir!!.absolutePath )  ///storage/emulated/0/Android/data/com.nufang.zao/cache

        return HttpProxyCacheServer.Builder(this)
            .maxCacheSize((1024 * 1024 * 1024 * 9).toLong()) // n个*1 Gb for cache
            .fileNameGenerator(MyFileNameGenerator())//自定义缓存文件名
            .build()


//        return HttpProxyCacheServer.Builder(this)
//            .maxCacheFilesCount(30)
//            .build()

    }

    /***
     * 边下边播
     * https://blog.csdn.net/sd19871122/article/details/79404123
     * https://gitcode.net/mirrors/danikula/AndroidVideoCache
     * */
    fun initVideoCacheProxy(): HttpProxyCacheServer? {
        return if (proxy == null) newCacheProxy().also { proxy = it }
        else proxy
    }

    private fun initXutils() {
        x.Ext.init(this)
        // 全局默认信任所有https域名 或 仅添加信任的https域名
//         使用RequestParams#setHostnameVerifier(...)方法可设置单次请求的域名校验
        x.Ext.setDefaultHostnameVerifier { hostname, session -> true }
    }

    companion object {

        private const val TAG = "ABpplication"
        var proxy: HttpProxyCacheServer? = null
    }
}